package com.iwhalecloud.bss.kite.cucc.order.accept.process;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.order.accept.process.enums.BusinessProcessorEnum;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 业务流程调度工厂类
 * @author xiao.xuzhang<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年2月12日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.order.accept.process <br>
 */
public abstract class OrderBusinessFactory {

    /**
     * 触发调度流程
     * @param orderRequest
     */
    public static void doHandle(OrderRequest orderRequest) {
        getProcessor(compute(orderRequest)).doHandle(orderRequest);
    }

    /**
     * 获取流程调度器
     * @param processorEnum
     * @return
     */
    public static IOrderBusinessProcessor getProcessor(BusinessProcessorEnum processorEnum) {
        return SpringUtil.getBean(processorEnum.getClazz());
    }

    /**
     * Description: <br>
     * 根据服务提供计算出真正业务流程器
     * @author xiao.xuzhang<br>
     * @taskId <br>
     * @param request
     * @return <br>
     */
    private static BusinessProcessorEnum compute(OrderRequest orderRequest) {
        // 默认返回通用处理器
        BusinessProcessorEnum processorEnum = BusinessProcessorEnum.COMPLEX;

        // 撤单服务流程
        if (isRevokeOrderServ(orderRequest)) {
            processorEnum = BusinessProcessorEnum.REVOKEORDER;
        }

        return processorEnum;
    }

    /**
     * Description: <br>
     * 判断是否是撤单业务
     * @author xiao.xuzhang<br>
     * @taskId <br>
     * @return <br>
     */
    private static boolean isRevokeOrderServ(OrderRequest request) {
        return KiteStringUtils.equals(ServiceOfferConsts.SERVICE_OFFER_REVOKE, request.getServiceOfferId());
    }
}
